Log4j Interview Questions and Answers
Ques 31. What are the format characters used in log4j?
The format characters used in log4j are
- L – it is used to output the line number from where the logging request was processed or issued
- m – It is used to output the application-supplied message related to the logging event
- p – It is used to output the priority of the logging event
- C – It is used to output the class name of the caller issuing the logging request
When any number is used along with the character it means the priority of the logging event should be justified to a width of 4 characters.
Ques 32. Mention what is the best way to migrate from java.util logging to apache log4j?
The best way to migrate from java.util logged to log4j is to use the global file search/replace method. It will replace with “org.apache.log4j.Logger”.
Then verify the project with the build.
Ques 33. Why do you get multiple copies of the message in log file sometime?
There could be two reasons why this might happen:
- Repeated configuration of log4j
- Attaching the same appenders to multiple loggers
Ques 34. How do you define logging for your application?
To define logging for your application, you have to download the log4j framework (log4j.jar) from the apache site.
Once log4j jars are downloaded, make sure that these jars are in classpath of your application. lets say you have web-application need to be added log4j. In this case, log4j jars are copied to WEB-INF/lib folder of your web application.
create new file either logging.properties or log4j.xml which will be copied to WEB-INF/classes folder.
logging.properties/log4j.xml contains the all the configuration related to logging mechanism and logger level and package that you want to define logger level.
Ques 35. What are different types of logs?
Usually in any application there two types of logs.
1. Application server logs:- These are the logs configured at the application server level. for example in tomcat, we have log files called localhost.log, tomcat.log, catalina.log, stdout.log, sterr.log. all these logs are showing with default settings defined in logging.properites located in your tomcat installation folder/conf folder.
If you want custom settings, we have to change the different parameters in logging.properties in conf folder of tomcat directory.
2. Application logs:- We can define logging at each applicaiton level, For this we have to create log4j.xml or logging.properties in WEB-INF/classes folder.
Most helpful rated by users:
- What is Log4j?
- What are the different logging levels?
- Describe about logger levels in log4j.
- What are the three main components in log4j?
- What are different appenders that can configure in log4j?